package com.agileboot.domain.airport.danger.model;

import cn.hutool.core.bean.BeanUtil;
import com.agileboot.common.exception.ApiException;
import com.agileboot.common.exception.error.ErrorCode;

import com.agileboot.domain.airport.danger.command.AddDangerSourceCommand;
import com.agileboot.domain.airport.danger.command.UpdateDangerSourceCommand;
import com.agileboot.domain.airport.danger.db.DangerSourceEntity;
import com.agileboot.domain.airport.danger.db.DangerSourceService;
import lombok.NoArgsConstructor;

@NoArgsConstructor
public class DangerSourceModel extends DangerSourceEntity {

    private DangerSourceService dangerSourceService;

    public DangerSourceModel(DangerSourceService dangerSourceService) {
        this.dangerSourceService = dangerSourceService;
    }

    public DangerSourceModel(DangerSourceEntity entity, DangerSourceService service) {
        BeanUtil.copyProperties(entity, this);
        this.dangerSourceService = service;
    }

    public void loadFromAddCommand(AddDangerSourceCommand command) {
        BeanUtil.copyProperties(command, this, "dangerSourceId");
    }

    public void loadFromUpdateCommand(UpdateDangerSourceCommand command) {
        loadFromAddCommand(command);
    }

    public void checkNameUnique() {
        if (dangerSourceService.isNameDuplicated(getDangerSourceId(), getDangerSourceName())) {
            throw new ApiException(ErrorCode.Business.DANGER_SOURCE_NAME_IS_NOT_UNIQUE, getDangerSourceName());
        }
    }
}
